package com.rd.student.ruanjian.class3.gaochenxi.day0728.before.ThreadPrac;

public class 打印ABC {
      public static void main(String[] args) {
            //创建三个线程，然后把三个任务分别放入这三个线程执行

            //创建线程1执行任务1
            new Thread(new A()).start();
            //创建线程2执行任务2
            new Thread(new B()).start();
            //创建线程3执行任务4
            new Thread(new C()).start();
      }

      private static final Object obj = new Object();
      private static int count = 1;

      static class A implements Runnable {

            @Override
            public void run() {
                  synchronized (obj) {
                        for (int i = 0; i < 10; i++) {
                              while (count % 3 != 1) {
                                    try {
                                          obj.wait();
                                    } catch (InterruptedException e) {
                                          throw new RuntimeException(e);
                                    }
                              }
                              obj.notifyAll();
                              System.out.println("A");
                              count++;
                        }
                  }
            }
      }

      static class B implements Runnable {

            @Override
            public void run() {
                  synchronized (obj) {
                        for (int i = 0; i < 10; i++) {
                              while (count % 3 != 2) {
                                    try {
                                          obj.wait();
                                    } catch (InterruptedException e) {
                                          throw new RuntimeException(e);
                                    }
                              }
                              obj.notifyAll();
                              System.out.println("B");
                              count++;
                        }
                  }
            }
      }

      static class C implements Runnable {
            @Override
            public void run() {
                  synchronized (obj) {
                        for (int i = 0; i < 10; i++) {
                              while (count % 3 != 3) {
                                    try {
                                          obj.wait();
                                    } catch (InterruptedException e) {
                                          throw new RuntimeException(e);
                                    }
                              }
                              obj.notifyAll();
                              System.out.println("C");
                              count++;
                        }
                  }
            }
      }
}

